﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Yule8xCommon.Applib
{
    public class ResponseUtil
    {
        public static HttpResponse Response { get { return System.Web.HttpContext.Current.Response; } }
        public static void Write(string msg)
        {
            Response.Write(msg);
        }
        public static void WriteEnd(string msg)
        {
            Response.Write(msg);
            Response.End();
        }
        public static void AjaxWrite(string msg, string type = "json", string charset = "utf-8")
        {
            string contentType = null;
            switch (type.ToLower())
            {
                case "json":
                    contentType = "application/json; charset=" + charset;
                    break;
                case "xml":
                    contentType = "text/xml; charset=" + charset;
                    break;
                case "jsonp":
                    contentType = "application/json; charset=" + charset;
                    string callback = RequestUtil.GetString("VAR_JSONP_HANDLER");
                    msg = string.Format("{0}({1})", callback, msg);
                    break;
            }
            Response.ContentType = contentType;
            Response.Write(msg);
            Response.End();
        }

        public static void OutputDictionary<T1, T2>(Dictionary<T1, T2> dict,string splitor="|")
        {
            StringBuilder sbld = new StringBuilder();
            foreach (T1 key in dict.Keys) {
                sbld.AppendFormat("{0}:{1}{2}",key,dict[key],splitor);
            }
            Response.Write(sbld.ToString());
        }

        public static void Alert(string value,bool end=false)
        {
            Response.Write(string.Format("<script>alert(\"{0}\");</script>",value.Replace('"',' ')));
            if (end)
            {
                Response.End();
            }
        }

        public static void JsLog(string value)
        {
            Response.Write(string.Format("<script>console.log(\"{0}\");</script>",value));
        }
    }
}